package com.lovo.roomProject.classManager.frame;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.Collections;
import java.util.List;

import javax.swing.*;

import com.lovo.netCRM.component.LovoButton;
import com.lovo.netCRM.component.LovoDate;
import com.lovo.netCRM.component.LovoTable;
import com.lovo.netCRM.component.LovoTitleLabel;
import com.lovo.netCRM.component.LovoTxt;
import com.lovo.roomProject.classManager.dao.ClassDAO;
import com.lovo.roomProject.classManager.dao.impl.ClassDAOImpl;
import com.lovo.roomProject.classManager.service.ClassService;
import com.lovo.roomProject.classManager.service.impl.ClassServiceImpl;
import com.lovo.roomProject.pojo.ClaszDO;

/**
 * 宿舍管理系统
 *
 * @author 张成峰
 * @version 1.0
 * @description 添加班级对话框
 * @see
 */
public class ClassAddDialog extends JDialog {
    private ClassService service = new ClassServiceImpl();


    /**
     * 班级表格
     */
    private LovoTable classTable;
    /**
     * 班级名称文本框
     */
    private LovoTxt nameTxt = new LovoTxt("班级名称", 50, 120, this);
    /**
     * 带班老师文本框
     */
    private LovoTxt teacherTxt = new LovoTxt("带班老师", 50, 180, this);
    /**
     * 开班时间日历框
     */
    private LovoDate createTimeTxt = new LovoDate("开班时间", 50, 260, this);
    /**
     * 班级面板
     */
    private ClassManagerPanel classManagerPanel;

    public ClassAddDialog(JFrame jf, ClassManagerPanel classManagerPanel) {
        super(jf, true);
        this.classManagerPanel = classManagerPanel;
        this.setLayout(null);

        this.init();

        Dimension screensize = Toolkit.getDefaultToolkit().getScreenSize();
        int x = (int) screensize.getWidth() / 2 - 200;
        int y = (int) screensize.getHeight() / 2 - 240;
        this.setBounds(x, y, 400, 480);
        this.setVisible(true);
    }

    /**
     * 初始化
     */
    private void init() {
        LovoTitleLabel titleLabel = new LovoTitleLabel("添加班级", this);
        titleLabel.setLocation(140, titleLabel.getY());
        LovoButton addButton = new LovoButton(40, 320, "添加", "image/add.png", this);
        addButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                boolean isOk = addClass();
                if (isOk) {
                    ClassAddDialog.this.dispose();
                }
            }
        });

        LovoButton cancelButton = new LovoButton(220, 320, "取消", "image/cancel.png", this);
        cancelButton.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                ClassAddDialog.this.dispose();
            }
        });
    }

    //---------------------

    /**
     * 添加班级
     */
    private boolean addClass() {
        //验证数据，验证失败，返回false
        String name = nameTxt.getText();

        if (this.nameTxt.getText().equals("")) {
            JOptionPane.showMessageDialog(null, "班级名不能为空");
            return false;
        }




        if (!this.teacherTxt.getText().matches("[a-zA-z\\u4e00-\\u9fa5]{2,20}")) {

            JOptionPane.showMessageDialog(null, "开班老师不能为空且必须是两位字母或汉字");
            return false;
        }


        if (this.createTimeTxt.getText().equals("")) {

            JOptionPane.showMessageDialog(null, "日期不能为空");
            return false;


        }

        if (service.getClassByName(name) != null) {
            JOptionPane.showMessageDialog(null, "班级名称重复！");
            return false;
        }






        ClaszDO claszDO = new ClaszDO();

        claszDO.setName(nameTxt.getText());
        claszDO.setTeacher(teacherTxt.getText());
        claszDO.setDate(LocalDate.parse(createTimeTxt.getText()));

        service.insert(claszDO);


        //更新表格，显示添加结果
        this.classManagerPanel.initData();

        return true;
    }
}
